package com.slg.android.manyanime.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.slg.android.manyanime.CartoonDetailActivity;
import com.slg.android.manyanime.R;
import com.slg.android.manyanime.ReadCartoonActivity;
import com.slg.android.manyanime.bean.Hua;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Thinkpad on 2015/3/13.
 */
public class HuaAdapter extends BaseAdapter {

    private Context context;
    private List<Hua> data;
    private int type;
    private String cartName;

    public HuaAdapter(Context context, List<Hua> data, int type, String cartName) {
        this.context = context;
        this.data = data;
        this.type = type;
        this.cartName = cartName;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final LayoutInflater inflater = LayoutInflater.from(context);
        View view = null;
        if (convertView != null) {
            view = convertView;
        } else {
            view = inflater.inflate(R.layout.item_cartoon_detaisl_grid, parent, false);
        }
        ViewHolder viewHolder = (ViewHolder) view.getTag();
        if (viewHolder == null) {
            viewHolder = new ViewHolder();
            viewHolder.huabt = (TextView) view.findViewById(R.id.item_cartoon_detail_grid_button);
            view.setTag(viewHolder);
        }
        if (!TextUtils.isEmpty(data.get(position).getName())) {
            viewHolder.huabt.setText(data.get(position).getName());
        }


        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ArrayList<String> ids = new ArrayList<String>();
                for (int i = 0; i < data.size(); i++) {
                    ids.add(data.get(i).getId());
                }

                Intent intent = new Intent(context, ReadCartoonActivity.class);
                intent.putExtra("hualist", ids);
                intent.putExtra("hua", position + 1);
                intent.putExtra("huaName",data.get(position).getName());
                intent.putExtra("type", getType());
                intent.putExtra("cartName", cartName);
                ((CartoonDetailActivity) context).startActivityForResult(intent, 1);

            }
        });


        return view;
    }


    public class ViewHolder {
        private TextView huabt;

    }
}
